package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.QueryPageBean;
import com.itheima.pojo.Result;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    //创建dao层代理对象
    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 添加检查项
     * @param checkItem
     * @return
     */
    @Override
    public Result addCheckItem(CheckItem checkItem) {
        //System.out.println("后台接收的数据:"+checkItem);
        int row = checkItemMapper.addCheckItem(checkItem);
        //System.out.println(row);
        if (row == 0){
            System.out.println("添加检查项失败");
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }else{
            System.out.println("添加检查项成功");
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }
    }

    @Override
    public Result findByPage(QueryPageBean queryPageBean) {
        //创建Map集合,存放数据集合,总条数
        Map<String, Object> map = new HashMap();
        //String name = queryPageBean.getQueryString();
        //name = "红";
        //System.out.println("模糊查询的条件:"+queryPageBean.getQueryString());
        //使用mybatis的分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //掉用dao方法
        List<CheckItem> list = checkItemMapper.findByPageByQueryString(queryPageBean.getQueryString());
        //List<CheckItem> list = checkItemMapper.findByPage();
        //System.out.println("这是模糊查询"+list);
        if (list == null) {
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }else{
            PageInfo<CheckItem> pageInfo = new PageInfo(list);
            //将数据存到集合中
            map.put("list",pageInfo.getList());
            map.put("total",pageInfo.getTotal());
            //System.out.println(map);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,map);
        }
    }

    @Override
    public Result deleteCheckItemById(String strId) {
        int id = 0;
        if (strId != null) {
            id = Integer.parseInt(strId);
        }
        //调用dao层方法
        int row = checkItemMapper.deleteCheckItemById(id);
        //System.out.println(id+"删除了"+row);
        if (row == 0){
            //删除失败
            System.out.println("删除失败");
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }else{
            System.out.println("删除成功");
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }
    }

    @Override
    public Result findCheckItemById(String strId) {
        int id = 0;
        if (strId != null) {
            id = Integer.parseInt(strId);
        }
        //调用dao层方法
        CheckItem checkItem = checkItemMapper.findCheckItemById(id);

        if (checkItem == null){
            System.out.println("检查项不存在");
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }else{
            System.out.println("查询成功");
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }
    }

    @Override
    public Result updateCheckItem(CheckItem checkItem) {
        int row = checkItemMapper.updateCheckItem(checkItem);
        //System.out.println(row);
        if (row == 0){
            System.out.println("修改检查项失败");
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }else{
            System.out.println("修改检查项成功");
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }
    }

    @Override
    public Result findAllCheckItem() {
        //获取检查项数据
        List<CheckItem> list = checkItemMapper.findAllCheckItem();
        if (list == null) {
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }else{
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }
    }
}
